package com.koron.css2.newProjectCenter.callback;

import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.newProjectCenter.Enum.PjCollectionPaymentStatus;
import com.koron.css2.newProjectCenter.bean.PjCollection;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.query.PjCollectionQuery;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.newProjectCenter.service.PjInfoService;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 预算审批节点回调
 */
@Service(PjInfoService.BUDGET_APPROVAL)
public class BudgetApprovalCallback implements IPjInfoNodeCallback{

    @Override
    public MessageBean callback(SessionFactory factory, PjInfoBean pjInfoBean, UserInfoBean userInfo) {
        if(pjInfoBean.getDepositMoney() != null && pjInfoBean.getDepositMoney() > 0){
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);
            CtmInfoBean ctmInfo = ctmInfoMapper.getCtmInfoById(pjInfoBean.getUserCtmTempId(), CtmInfoBean.CTMTEMP);
            PjCollection pjCollection = new PjCollection();
            pjCollection.setPjId(pjInfoBean.getId());
            pjCollection.setCreateInfo(userInfo);
            pjCollection.setStatus(PjCollectionPaymentStatus.BEFORECOLLECTION.getStatus());
            pjCollection.setProjectCode(pjInfoBean.getBillNo());
            pjCollection.setProjectAddress(pjInfoBean.getInstallAddr());
            pjCollection.setConstructionUnit(pjInfoBean.getConstructionUnit());
            pjCollection.setProjectDate(pjInfoBean.getBillDate());
            pjCollection.setProjectName(pjInfoBean.getPjName());
            pjCollection.setType(PjCollection.TYPE_1); // 预收款
            if (ctmInfo != null){
                pjCollection.setClientName(ctmInfo.getCtmName());
            }
            pjCollection.setBillDate(new Date());
            pjCollection.setBillNo(Tools.getBillNo(factory, "pj_collection"));
            pjCollection.setMoney(new BigDecimal(pjInfoBean.getBudgetMoney() == null ? 0 : pjInfoBean.getBudgetMoney()));
            // 查询是否已经存在预算单
            PjCollectionQuery query = new PjCollectionQuery();
            query.setProjectCode(pjInfoBean.getBillNo());
            query.setExcludeStatus(PjCollectionPaymentStatus.FQ.getStatus());
            query.setType(PjCollection.TYPE_1);
            PjCollection single = pjCollectionMapper.getSingle(query);
            // 假如工程收款单不存在
            if(single == null) {
                pjCollectionMapper.insert(pjCollection);
            }
            // 假如工程收款单据已存在并且不是已收款状态
            if (single != null && single.getStatus() != PjCollection.STATUS_2){
                // 设置工程收款单据主键 ID
                pjCollection.setId(single.getId());
                pjCollectionMapper.update(pjCollection);
            }
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);
    }
}
